home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-11-03 | 10.9 KB | 400 lines |
- package com.symantec.itools.tools.archive;
-
-
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStreamReader;
- import java.io.IOException;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Vector;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipFile;
- import com.symantec.itools.lang.Classpath;
- import com.symantec.itools.lang.ClasspathEntry;
- import com.symantec.itools.lang.JavaName;
- import com.symantec.itools.lang.NotJavaNameException;
- import com.symantec.itools.io.FileSystem;
- import com.symantec.itools.io.Directory;
- import com.symantec.itools.io.NotDirectoryException;
- import com.symantec.itools.lang.Debug;
-
-
- /**
- * @author Symantec Internet Tools Division
- * @version 1.0
- * @since VCafe 3.0
- */
-
- public class DirectoryArchiver
- extends TypeArchiver
- {
- public DirectoryArchiver(Options options)
- {
- super(options);
- }
-
- /**
- * @param errorMsg TODO
- * @since VCafe 3.0
- */
-
- public boolean create(StringBuffer errorMsg)
- {
- String outDir;
- Classpath classpath;
- Directory directory;
-
- outDir = options.getOutputLocation();
- classpath = new Classpath(options.getClasspath());
-
- try
- {
- directory = new Directory(outDir, true);
- }
- catch(NotDirectoryException ex)
- {
- errorMsg.append(outDir).append(" is a file not a directory");
-
- return (false);
- }
- catch(FileNotFoundException ex)
- {
- errorMsg.append(outDir).append(" cannot be created");
-
- return (false);
- }
- catch(IOException ex)
- {
- errorMsg.append("Error creating " + outDir);
-
- return (false);
- }
-
- outDir = FileSystem.getCanonicalPath(outDir, true);
-
- if(!(copyFiles(outDir, options.getFiles(), classpath, errorMsg)))
- {
- return (false);
- }
-
- return (copyEntries(outDir, classpath, errorMsg));
- }
-
- /**
- * @param outDir TODO
- * @param files TODO
- * @param classpath TODO
- * @param errorMsg TODO
- * @since VCafe 3.0
- */
-
- protected boolean copyFiles(String outDir, String[] files, Classpath classpath, StringBuffer errorMsg)
- {
- for(int i = 0; i < files.length; i++)
- {
- File file;
- BufferedInputStream inStream;
- BufferedOutputStream outStream;
-
- file = new File(files[i]);
- inStream = null;
- outStream = null;
-
- try
- {
- if(file.exists() && file.isFile())
- {
- File outFile;
- File outDirs;
- byte[] buf;
- int len;
- String outName;
-
- outName = outDir + classpath.convertFileNameToJavaName(files[i]).getFullName().replace('/', File.separatorChar);
- outFile = new File(outName);
- outDirs = new File(outFile.getParent());
-
- if(verbose)
- {
- // start event
- }
-
- if(!(outDirs.exists()))
- {
- if(!(outDirs.mkdirs()))
- {
- errorMsg.append("unable to create " + outDirs);
-
- return (false);
- }
- }
-
- inStream = new BufferedInputStream(new FileInputStream(file));
- outStream = new BufferedOutputStream(new FileOutputStream(outFile));
- buf = new byte[1024];
-
- while((len = inStream.read(buf, 0, 1024)) != -1)
- {
- outStream.write(buf, 0, len);
- outStream.flush();
- }
-
- if(verbose)
- {
- // end event
- }
- }
- }
- catch(IOException ex)
- {
- Debug.logException(ex);
- errorMsg.append("Error copying " + file);
-
- return (false);
- }
- catch(NotJavaNameException ex)
- {
- Debug.logException(ex);
- errorMsg.append("Invalid entry " + file);
-
- return (false);
- }
- finally
- {
- if(inStream != null)
- {
- try
- {
- inStream.close();
- }
- catch(IOException ex)
- {
- Debug.logException(ex);
- }
- }
-
- if(outStream != null)
- {
- try
- {
- outStream.flush();
- outStream.close();
- }
- catch(IOException ex)
- {
- Debug.logException(ex);
- }
- }
- }
- }
-
- return (true);
- }
-
- /**
- * @param outDir TODO
- * @param classpath TODO
- * @param errorMsg TODO
- * @since VCafe 3.0
- */
-
- protected boolean copyEntries(String outDir, Classpath classpath, StringBuffer errorMsg)
- {
- String[] entries;
- Hashtable archives;
- Vector files;
-
- entries = options.getEntries();
- archives = new Hashtable();
- files = new Vector();
-
- for(int i = 0; i < entries.length; i++)
- {
- ClasspathEntry entry;
-
- entry = classpath.find(entries[i]);
-
- if(entry != null)
- {
- if(entry.isFile())
- {
- Vector list;
-
- if(!(archives.containsKey(entry)))
- {
- archives.put(entry, new Vector());
- }
-
- list = (Vector)archives.get(entry);
- list.addElement(entries[i]);
- }
- else
- {
- files.addElement(FileSystem.getCanonicalPath(entry.getName(), true) + entries[i].replace('/', File.separatorChar));
- }
- }
- else
- {
- errorMsg.append("Cannot find " + entries[i]);
- classpath.cleanup();
-
- return (false);
- }
- }
-
- classpath.cleanup();
-
- if(archives.size() > 0)
- {
- for(Enumeration e = archives.keys(); e.hasMoreElements();)
- {
- ClasspathEntry key;
- Vector list;
- String[] array;
-
- key = (ClasspathEntry)e.nextElement();
- list = (Vector)archives.get(key);
- array = new String[list.size()];
- list.copyInto(array);
-
- if(!(copyArchiveEntries(outDir, key.getName(), array, errorMsg)))
- {
- return (false);
- }
- }
- }
-
- if(files.size() > 0)
- {
- String[] array;
-
- array = new String[files.size()];
- files.copyInto(array);
-
- if(!(copyFiles(outDir, array, classpath, errorMsg)))
- {
- return (false);
- }
- }
-
- return (true);
- }
-
- /**
- * @param outDir TODO
- * @param name TODO
- * @param entries TODO
- * @param errorMsg TODO
- * @since VCafe 3.0
- */
-
- protected boolean copyArchiveEntries(String outDir, String name, String[] entries, StringBuffer errorMsg)
- {
- ZipFile zip;
-
- try
- {
- zip = new ZipFile(name);
- }
- catch(IOException ex)
- {
- Debug.logException(ex);
- errorMsg.append("Can't open " + name);
-
- return (false);
- }
-
- for(int i = 0; i < entries.length; i++)
- {
- BufferedInputStream inStream;
- BufferedOutputStream outStream;
-
- inStream = null;
- outStream = null;
-
- try
- {
- File outFile;
- File outDirs;
- byte[] buf;
- int len;
- String outName;
- ZipEntry entry;
- long remaining;
-
- outName = FileSystem.getCanonicalPath(outDir, true) + entries[i].replace('/', File.separatorChar);
- outFile = new File(outName);
- outDirs = new File(outFile.getParent());
-
- if(!(outDirs.exists()))
- {
- if(!(outDirs.mkdirs()))
- {
- errorMsg.append("unable to create " + outDirs);
-
- return (false);
- }
- }
-
- entry = zip.getEntry(entries[i]);
- inStream = new BufferedInputStream(zip.getInputStream(entry));
- outStream = new BufferedOutputStream(new FileOutputStream(outFile));
- buf = new byte[1024];
- remaining = entry.getSize();
-
- while((len = inStream.read(buf, 0, (int)Math.min(remaining, 1024))) != -1)
- {
- outStream.write(buf, 0, len);
- remaining -= len;
- }
- }
- catch(IOException ex)
- {
- Debug.logException(ex);
- errorMsg.append("Error copying " + entries[i] + "to " + outDir);
-
- return (false);
- }
- finally
- {
- if(inStream != null)
- {
- try
- {
- inStream.close();
- }
- catch(IOException ex)
- {
- }
- }
-
- if(outStream != null)
- {
- try
- {
- outStream.close();
- }
- catch(IOException ex)
- {
- }
- }
- }
- }
-
- return (true);
- }
-
- /**
- * @since VCafe 3.0
- */
-
- public void cancel()
- {
- // TODO - implement cancel functionality
- }
- }